package com.it.jzs.controller.admin;

import com.it.jzs.pojo.Type;
import com.it.jzs.pojo.User;
import com.it.jzs.service.admin.AdminUserService;
import com.it.jzs.vo.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 后台简单的crud
 */
@RestController
@RequestMapping("admin")
@CrossOrigin
public class AdminUserController {
    @Resource
    private AdminUserService adminUserService;

    @GetMapping("user")
    public Result<Object> ListUserPage(@RequestParam("index") Integer index, @RequestParam("size") Integer size,@RequestParam("keyWords") String keyWords) {  // 分页查询
//        System.out.println("index = " + index);
        Result<Object> result = adminUserService.selectPage(index, size,keyWords);
//        System.out.println("result = " + result);
        return result;
    }


    @PostMapping("user")
    public Result<Type> addUser(@RequestBody User user) {  // 新增
//        System.out.println("type = " + type);
//        System.out.println("user = " + user);
        Result result = adminUserService.insertOne(user);
//        adminTypeService.insertOne(type);
        return result;
    }

    @DeleteMapping("user/{id}")
    public Result<Object> deleteType(@PathVariable("id") Integer id) {  // 删除用户
//        System.out.println("id = " + id);
        Result<Object> result = adminUserService.delete(id);
        return result;
//        return Result.success(null, "测试");
    }

    @PutMapping("user")
    public Result<Object> updateType(@RequestBody User user) {
//        System.out.println("type = " + type);
//        System.out.println("user = " + user);
        Result<Object> result = adminUserService.update(user);
        return result;
//        return Result.success(null, "cs");
    }

    @PutMapping("user/{id}/{type}")
    public Result<Object> deleteType(@PathVariable("id") Integer id, @PathVariable("type") Integer type) {  // 修改用户状态
//        System.out.println("id = " + id);
//        System.out.println("type = " + type);
        Result<Object> result = adminUserService.updateStatus(id, type);
//        Result<Object> result = adminUserService.delete(id);
        return result;
//        return Result.success(null, "测试");
    }
    @PostMapping("user/deletebatch")
    public Result<Object> deleteBatch(@RequestBody List<Integer> ids) {  // 批量删除
        Result<Object> result = adminUserService.deleteBatch(ids);

        return result;
    }
}
